package com.zzwtec.information.controller;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.zzwtec.information.model.News;
import com.zzwtec.information.dao.NewsDao;
import com.zzwtec.interfaces.ResultObject;

import com.zzwtec.common.util.BeanUtil;
import com.zzwtec.common.util.ErrorMsg;
import com.zzwtec.interfaces.Page;
import com.zzwtec.interfaces.RequestObject;
import com.zzwtec.interfaces.information.dto.NewsDTO;
import com.zzwtec.interfaces.information.services.NewsService;


/**
 * 微服务NewsService接口实现类
 * @author  草原狼
 * 
 * 2017年08月07日
 */

@RestController
public class NewsController implements NewsService {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	@Resource(name = "newsDao")
	private NewsDao newsDao;

	@Override
	public ResultObject<String> addNews(@RequestBody RequestObject<NewsDTO> request) {
		ResultObject<String> result = new ResultObject<String>(request);
		NewsDTO dto = request.getData();
		News bean = new News();
		BeanUtil.copyProperties(bean, dto);		
		newsDao.insert(bean);
		result.setData(bean.getId());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}
	
	@Override
	public ResultObject<Void> removeNews(@RequestBody RequestObject<String> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		newsDao.deleteById(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}
	
	@Override
	public ResultObject<Void> removeNewsByIds(RequestObject<String[]> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		newsDao.deleteByIds(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	

	@Override
	public ResultObject<Void> modifyNews(@RequestBody RequestObject<NewsDTO> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		NewsDTO dto = request.getData();
		News bean = new News();
		BeanUtil.copyProperties(bean, dto);		
		newsDao.update(bean);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}


	@Override
	public ResultObject<List<NewsDTO>> queryAllNews(@RequestBody RequestObject<Void> request) {
		ResultObject<List<NewsDTO>> result = new ResultObject<List<NewsDTO>>(request);
		List<News> list = newsDao.selectAll();
		List<NewsDTO> data = new ArrayList<NewsDTO>();
		for(News entity : list){
			NewsDTO dto = new NewsDTO();
			BeanUtil.copyProperties(dto, entity);
			data.add(dto);
		}
		result.setData(data);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}


	@Override
	public ResultObject<NewsDTO> queryNewsById(@RequestBody RequestObject<String> request) {
		ResultObject<NewsDTO> result = new ResultObject<NewsDTO>(request);
		News entity = newsDao.selectOne(request.getData());
		NewsDTO dto = new NewsDTO();
		BeanUtil.copyProperties(dto, entity);
		result.setData(dto);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}

	@Override
	public ResultObject<Page<NewsDTO>> queryNewsByPage(@RequestBody RequestObject<Page<NewsDTO>> request) {
		ResultObject<Page<NewsDTO>>  result=new ResultObject<Page<NewsDTO>>(request);
		Page<NewsDTO> page=request.getData();
		Page<News> rPage=new Page<News>();
		rPage.setCondition(page.getCondition());
		rPage.setDistinct(page.isDistinct());
		rPage.setGroupByFields(page.getGroupByFields());
		rPage.setOrderField(page.getOrderField());
		rPage.setOrderType(page.getOrderType());
		rPage.setPage(page.getPage());
		rPage.setPageNum(page.getPageNum());
		rPage.setSearchFields(page.getSearchFields());
		rPage.setSearchValues(page.getSearchValues());
		rPage = newsDao.selectPage(rPage);
		if(rPage.getData()!=null){
			List<NewsDTO> data = new ArrayList<NewsDTO>(rPage.getData().size());
			for(News entity : rPage.getData()){	
				NewsDTO dto = new NewsDTO();
				BeanUtil.copyProperties(dto, entity);
				data.add(dto);
			}
			page.setData(data);
		}
		page.setTotal(rPage.getTotal());
		page.setRows(rPage.getRows());
		result.setData(page);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	
	
	@Override
	public ServiceInstance showServiceInfo() {		
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
